package com.elock.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.dao.impl.DaoCenter;
import com.elock.base.core.util.ConvertUtil;
import com.elock.base.core.util.WebUtils;

/**
 * Dao - 锁
 * @author 仁德
 */
@Repository("warningMessageProcessDao")
public class WarningMessageProcessDao extends DaoCenter {

	public Page<Map<String, Object>> findPage(String terminalNum, String elockNum,
			String carNum, Integer flag, String version, Pageable pageable) {

		List<Object> list = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder();
		Long companyInfoId = WebUtils.getCurrentCompanyInfoId();
		sql.append("select w.*,e.car_number,e.position,e.seq,e.berth");
		sql.append(
				" from warning_message_process w left join elock_car e on w.elock_num=e.elock_num and w.company_info_id=e.company_info_id where 1=1");
		if (companyInfoId != null) {
			sql.append(" and w.company_info_id=?");
			list.add(companyInfoId);
		}
		if (flag != null && flag == 1) { // 1只查未处理
			sql.append(" and w.is_process=0");
		}
		if (!ConvertUtil.isEmpty(elockNum)) {
			sql.append(" and w.elock_num like ?");
			list.add("%" + elockNum + "%");
		}
		if (!ConvertUtil.isEmpty(terminalNum)) {
			sql.append(" and w.terminal_num like ?");
			list.add("%" + terminalNum + "%");
		}
		if (!ConvertUtil.isEmpty(carNum)) {
			sql.append(" and e.car_number like ?");
			list.add("%" + carNum + "%");
		}
		if (!ConvertUtil.isEmpty(version)) {
			sql.append(" and w.version like ?");
			list.add("%" + version + "%");
		}

		sql.append(" order by w.create_date desc");

		Object[] objs = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			objs[i] = list.get(i);
		}

		Page<Map<String, Object>> getPage = getNativeDao().findPageMap(sql.toString(),
				objs,
				pageable);

		return getPage;

	}

}